﻿using System.Linq.Expressions;
using System.Reflection;

namespace BrightSword.Feber.Core
{
    public abstract class BinaryOperationBuilderBase<TProto, TLeftInstance, TRightInstance> : OperationBuilderBase<TProto>
    {
// ReSharper disable StaticFieldInGenericType
        private static readonly ParameterExpression _leftParameterExpression = Expression.Parameter(typeof (TLeftInstance),
                                                                                                    "_left");

        private static readonly ParameterExpression _rightParameterExpression = Expression.Parameter(typeof (TRightInstance),
                                                                                                     "_right");

// ReSharper restore StaticFieldInGenericType
        protected virtual ParameterExpression LeftInstanceParameterExpression
        {
            get
            {
                return _leftParameterExpression;
            }
        }

        protected virtual ParameterExpression RightInstanceParameterExpression
        {
            get
            {
                return _rightParameterExpression;
            }
        }

        protected override Expression BuildPropertyExpression(PropertyInfo propertyInfo)
        {
            //try
            //{
            return PropertyExpression(propertyInfo,
                                      LeftInstanceParameterExpression,
                                      RightInstanceParameterExpression);
            //}
            //catch
            //{
            //    return Expression.Default(typeof (void));
            //}
        }

        protected abstract Expression PropertyExpression(PropertyInfo property,
                                                         ParameterExpression leftInstanceParameterExpression,
                                                         ParameterExpression rightInstanceParameterExpression);
    }
}